from socket import *


class Student:
    def __init__(self, sock):
        self.sock = sock

    def add_stu(self):
        stu_id = input("Id:")
        name = input("Name:")
        score = input("Score:")
        request = f"add {stu_id} {name} {score}"
        self.sock.send(request.encode())
        res = self.sock.recv(1024)
        if res == b"OK":
            print("success")
        else:
            print("defeat")

    def update_stu(self):
        stu_id = input("Id:")
        name = input("Name:")
        score = input("Score:")
        request = f"update {stu_id} {name} {score}"
        self.sock.send(request.encode())
        res = self.sock.recv(1024)
        if res == b"OK":
            print("success")
        else:
            print("defeat")

    def del_stu(self):
        stu_id = input("Id:")
        request = f"del {stu_id}"
        self.sock.send(request.encode())
        res = self.sock.recv(1024)
        if res == b"OK":
            print("success")
        else:
            print("defeat")


class NetWork:
    def __init__(self):
        self.server_address = ("127.0.0.1", 8888)
        self.sock = self._create_socket()
        self.stu = Student(self.sock)

    def _create_socket(self):
        sock = socket()
        sock.connect(self.server_address)
        return sock

    def run(self):
        while True:
            print("===============================================================")
            print("======================请选择你的操作=============================")
            print("===1,add student  2,update student  3,delete student  4,exit===")
            print("===============================================================")
            cmd = input(">>")
            if cmd == "1":
                self.stu.add_stu()
            elif cmd == "2":
                self.stu.update_stu()
            elif cmd == "3":
                self.stu.del_stu()
            elif cmd == "4":
                break
            else:
                print("输入错误！！！！！")


if __name__ == '__main__':
    client = NetWork()
    client.run()
